/**
 * 游戏场玩家界面
 */
class ZJHPlayerView extends how.module.View {
    public nickNameLabel: eui.Label;//用户昵称
    public moneyLabel: eui.Label;//用户筹码
    public avatar: eui.Image;//用户头像
    public operateResult: eui.Image;
    public isCardRight: boolean = true;
    public handCardPanel: eui.Group;
    public haveHandCardPanel: boolean = true;
    public bankIcon: eui.Image;
    private progressTimer: ZJHProgressTimer;//时间倒计时
    public clockTween: egret.Tween;//定时器特效
    public headContainer: eui.Group;
    public shengliEffect: how.Animation;
    public start(): void 
    {
        console.log(">>> ZJHPlayerView start ",this.isCardRight);
        if (this.isCardRight) {
            this.handCardPanel.right = Math.abs(this.handCardPanel.right);
            this.bankIcon.right = this.headContainer.width;
        }
        this.handCardPanel.visible = this.haveHandCardPanel;
        this.progressTimer.visible = false;
        this.bankIcon.visible = false;
        this.shengliEffect.visible = false;
        for (var i: number = 0; i < 3; i++) {
            this.handCardPanel.getChildAt(i).visible = false;
        }
    }
    public setBankUI(isBanker: boolean): void {
        this.bankIcon.visible = isBanker;
    }
    /**
    * 设置离开UI（包括起立、旁观、离开）
    */
    public setLeaveUI(): void {
        console.log(">>> ZJHPlayerView setLeaveUI 设置离开UI（包括起立、旁观、离开）");
        this.visible = false;
    }
    /**
    * 设置首次坐下UI
    */
    public setFirstSitUI(data: ZJHPlayerData): void 
    {
        console.log(">>> ZJHPlayerView setFirstSitUI 设置首次坐下UI");
        this.nickNameLabel.text = data.nickName;
        this.avatar.source = "avatar_" + Math.floor(new how.Random(data.id).range(0, 6)) + "_png";
        this.moneyLabel.text = base.Utils.formatCurrency(data.lTakeMoney);
        this.alpha = 0.5;
    }
    /*
     * 设置自己首次坐下UI
     * */
    public setMyFirstSitUI(): void
    {
        console.log(">>> ZJHPlayerView setMyFirstSitUI 设置自己首次坐下UI");
        this.nickNameLabel.text = LanguageConfig.waitNextFrame;
    }
    public setMoneyUI(data: ZJHPlayerData): void 
    {
        console.log(">>> ZJHPlayerView setMoneyUI ");
        this.moneyLabel.text = base.Utils.formatCurrency(data.lTakeMoney);
    }
    /**
    * 设置坐下UI（玩家进入坐下的状态）
    */
    public setSitUI(data: ZJHPlayerData): void 
    {
        console.log(">>> ZJHPlayerView setSitUI 设置坐下UI（玩家进入坐下的状态）");
        if (data) {
            this.visible = true;
            this.nickNameLabel.text = data.nickName;
            this.avatar.source = "avatar_" + Math.floor(new how.Random(data.id).range(0, 6)) + "_png";
            this.moneyLabel.text = base.Utils.formatCurrency(data.lTakeMoney);
            this.operateResult.visible = false;
            this.bankIcon.visible = false;      
            for (var i: number = 0; i < 3; i++) {
                this.handCardPanel.getChildAt(i).visible = false;
            }
        }
    }
    /**
    * 游戏开始
    * */
    public onGameStart(data: ZJHPlayerData): void 
    {
        console.log(">>> ZJHPlayerView onGameStart 游戏开始");
        this.alpha = 1;
        this.nickNameLabel.text = data.nickName;
        egret.Tween.get(this).to({ x: 205 }, 300);
        this.operateResult.visible = false;
        this.bankIcon.visible = false;
        for (var i: number = 0; i < 3; i++) {
            this.handCardPanel.getChildAt(i).visible = false;
        }
    }
    //设置游戏UI（玩家进入游戏的状态）
    public setGameUI(data: ZJHPlayerData): void 
    {
        this.operateResult.visible = false;
        this.bankIcon.visible = false;
        for (var i: number = 0; i < 3; i++) {
            var card: eui.Image = this.handCardPanel.getChildAt(i) as eui.Image;
            card.visible = false;
            card.source = "card_0_png";
        }
    }
    /**
     * 设置可以操作UI
     */
    public setOperateUI(operatorTime: number = 0): void 
    {
        console.log(">>> ZJHPlayerView setOperateUI 设置可以操作UI  ",operatorTime);
        this.progressTimer.visible = true;
        this.startClock(operatorTime);
    }
    /**
     * 设置操作结束UI
     */
    public setOperateOverUI(score: number): void
    {
        console.log(">>> ZJHPlayerView setOperateOverUI 设置操作结束UI");
        this.progressTimer.visible = false;
        this.stopClock();
        var score: number = score;
        var gameView: ZJHGameView = (<ZJHGameView>how.Application.currentScene);
        gameView.gameEffectPanel.playChoumaAddEffect(this, score, gameView.choumaArea);
    }
    /**
     * 设置轮次结束UI
     */
    public onRoundOver(score: number): void
    {
        console.log(">>> ZJHPlayerView onRoundOver 设置轮次结束UI");
        this.progressTimer.visible = false;
        this.stopClock();
    }
    //震动三次判断
    private _shakeIndex: number = -1;
    private _PLAY_CD: number = 12;
    /**
    * 开始定时器
    * @operatorTime 已操作时间
    */
    public startClock(operatorTime: number = 0): void 
    {
        console.log(">>> ZJHPlayerView startClock 开始定时器");
        this._shakeIndex = -1;
        if (this.clockTween) {
            this.clockTween.pause();
        }
        this.progressTimer.value = operatorTime / this._PLAY_CD * 100;
        if (this.progressTimer.value < 100) {
            this.clockTween = egret.Tween.get(this.progressTimer, { onChange: this.onRingJudge, onChangeObj: this }).
                to({ value: 100 }, (this._PLAY_CD - operatorTime) * 1000).//倒计时10秒
                call(this.onTimeOver, this);//第13秒倒计时结束;
        }
        else {
            this.onTimeOver();
        }
    }
    /**
     * 停止定时器
     */
    public stopClock(): void {
        if (this.clockTween) {
            this.clockTween.setPaused(true);
            this.clockTween = null;
        }
        this.progressTimer.value = 0;
        egret.Tween.removeTweens(this.progressTimer);
    }
    private onRingJudge(): void {
        if ((this.progressTimer.value >= (this._PLAY_CD - 1) / this._PLAY_CD * 100)) {
            if (this._shakeIndex != 2) {
                this._shakeIndex = 2;
                this.onTimeRing(this._shakeIndex);
            }
        }
        else if ((this.progressTimer.value >= (this._PLAY_CD - 2) / this._PLAY_CD * 100)) {
            if (this._shakeIndex != 1) {
                this._shakeIndex = 1;
                this.onTimeRing(this._shakeIndex);
            }
        }
        else if ((this.progressTimer.value >= (this._PLAY_CD - 3) / this._PLAY_CD * 100)) {
            if (this._shakeIndex != 0) {
                this._shakeIndex = 0;
                this.onTimeRing(this._shakeIndex);
            }
        }
    }
    /**
    * 倒计时警告
    * @param index {number} 第几次警告
    */
    public onTimeRing(index: number): void {
        this.report("onTimeRing", index);
    }
    /**
    * 倒计时结束
    */
    public onTimeOver(): void {
        this.stopClock();
        this.report("onClockStop");
    }
    public setLookCardUI(): void {
        this.operateResult.visible = true;
        this.operateResult.source = "zjh_yikanpai_png";
    }
    public setCompareUI(canOperate: boolean, isLoss: boolean): void {
        if (!canOperate) {
            this.progressTimer.visible = false;
            this.stopClock();
        }
        else {
            this.startClock();
        }
        if (isLoss) {
            this.operateResult.visible = true;
            this.operateResult.source = "zjh_bipaishu_png";
        }
    }

    /**
     * 设置游戏结束UI
     */
    public setGameEndUI(isLoss: boolean, cardData: number[]): void 
    {
        console.log(">>> ZJHPlayerView setGameEndUI 设置游戏结束UI",isLoss);
        for (var i: number = 0; i < cardData.length; i++) {
            var card: eui.Image = this.handCardPanel.getChildAt(i) as eui.Image;
            card.source = "card_" + cardData[i] + "_png";
        }
        if (!isLoss) {
            how.SoundManager.playEffect("zjh_gamewin_mp3");
            this.shengliEffect.visible = true;
            this.shengliEffect.play();
            this.shengliEffect.once(egret.Event.ENDED, () => {
                this.shengliEffect.visible = false;
                var cardTypeImg: eui.Image = new eui.Image();
                cardTypeImg.source = "zjh_cardType_" + ZJHCardLogic.getInstance().getCardType(cardData) + "_png";
                this.addChild(cardTypeImg);
                cardTypeImg.x = this.width / 2 - cardTypeImg.width / 2;
                cardTypeImg.y = this.height / 2 - cardTypeImg.height / 2;
                egret.Tween.get(cardTypeImg).to({ y: -cardTypeImg.height }, 300).wait(300).to({ alpha: 0 }, 300).call(() => {
                    this.removeChild(cardTypeImg);
                }, this);
                var gameView: ZJHGameView = (<ZJHGameView>how.Application.currentScene);
                gameView.gameEffectPanel.playChoumaWinEffect(this);
            }, this);
        }
        else {

        }
        
        this.nickNameLabel.text = LanguageConfig.waitNextFrame;
        this.operateResult.visible = false;
        this.bankIcon.visible = false;
        //for (var i: number = 0; i < 3; i++) {
        //     this.handCardPanel.getChildAt(i).visible = false;
        //}
    }
}